package com.unc.allergyex.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.unc.allergyex.R;
import com.unc.allergyex.objects.db.Restaurant;

/**This is the ArrayAdapter for the disambiguation screen.  
 * It is used by the Disambiguation activity to display the candidates for disambiguation.  
 * It uses the disambiguationitem.xml layout file.  
 * @author Stephanie
 *
 */
public class DisambiguateAdapter extends ArrayAdapter<Restaurant> {

	private ArrayList<Restaurant> rests;	

	/**Constructor for the adapter.  
	 * @param context the context into which the notifications should be made
	 * @param textViewResourceId default android, not used for anything
	 * @param items the list of components to display
	 */
	public DisambiguateAdapter(Context context, int textViewResourceId, ArrayList<Restaurant> items) {
		super(context, textViewResourceId, items);
		rests = items;
	}
	
	/**This is the method to set up the item view.
	 * It sets the name of the restaurant, the address, and the hidden id
	 * @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		//inflate the view into the context
		View v;
		LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
		v = vi.inflate(R.layout.disambiguationitem, null);

		Restaurant restaurant = rests.get(position);
		if (restaurant != null) {
			
			//set the name text
			TextView name = (TextView) v.findViewById(R.id.name);			
			if(name != null){
				name.setText(restaurant.getName());
			}

			//set the hidden restaurant id
			TextView id = (TextView) v.findViewById(R.id.restaurantIdD);
			id.setText(restaurant.getId());
			
			//set the address text
			TextView address = (TextView) v.findViewById(R.id.address);
			if (address != null){
				if (!restaurant.isChain()){
					address.setText(restaurant.getLocations()[0].getAddress());
				}
				else {
					address.setText("Chain Restaurant");
				}
			}
		}		
		return v;
	}

}
